﻿using System;
using System.Collections.Generic;
using System.Collections;
using UnityEngine;

namespace Battle.View
{
    public class PalaboraProjectile : Projectile
    {
        Vector3 initPosition;
        Vector3 targetPosition;

        public const float g = -9.8f;

        float initYSpeed;
        float xSpeed;
        float zSpeed;


        protected override void Shoot()
        {
            var targetBC = BattleViewCtrl.GetInstance().GetBattleCharacter(targetPos);
            if (targetBC != null)
            {
                if (!string.IsNullOrEmpty(projResData.targetBindBone))
                {
                    targetPosition = targetBC.GetBonePosition(projResData.targetBindBone);
                }
                else
                {
                    targetPosition = targetBC.GetBeFoundBonePosition();
                }
            }
            else
            {
                targetPosition = BattleViewCtrl.GetInstance().GetPosition(targetPos);
            }

            transform.SetParent(BattleViewCtrl.GetInstance().transform);
            transform.localPosition = creator.transform.localPosition + projResData.initOffset;
            transform.localScale = Vector3.one;
            transform.localEulerAngles = Vector3.zero;

            initPosition = transform.localPosition;

            initYSpeed = -0.5f * g * projResData.projFlyTime;

            xSpeed = (targetPosition.x - initPosition.x) / projResData.projFlyTime;
            zSpeed = (targetPosition.z - initPosition.z) / projResData.projFlyTime;

            deltaTime = 0f;

        }

        float deltaTime = 0f;


        protected override void Tick0()
        {
            deltaTime += Time.deltaTime;
            if (deltaTime <= projResData.projFlyTime)
            {
                float yPos = initYSpeed * deltaTime + 0.5f * g * deltaTime * deltaTime;
                float xPos = deltaTime * xSpeed + initPosition.x;
                float zPos = deltaTime * zSpeed + initPosition.z;
                var newPos = new Vector3(xPos, yPos, zPos);
                transform.LookAt(newPos);
                transform.localPosition = newPos;
            }
        }


    }
}
